//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<xdemo.cpp>
///	@path	~/src/xdemo/
///	@date	2007/09/08
///	@desc	A simple engine demo entry.

/*
	This file shows how the engine works & how to build games or demo from
	the engine tool set.
	

*/
#define _FULLAPI
// config
#include "config/config.h"
#include "config/os.h"

// xeres
#include "xeres.h"

// lib
#include "lib/utilities/string.h"
#include "lib/utilities/debug.h"
#include "lib/utilities/dir_tree.h"
#include "lib/utilities/observer.h"
#include "lib/system/thread.h"

// app
#include "appear/xwin/SplashScreen.h"

// engine
#include "engine/xcore/xeres_engine.h"

// other
#include <tchar.h>
#include <DxErr.h>
#include <gdiplus.h>

// xdemo
#include "XDemoApp.h"

using namespace xeres;
using namespace Gdiplus;

//#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.Common-Controls' version='8.0.50608.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

// initial game title
static const WString APP_NAME = _S("xeres_engine_demo");
static const WString APP_TITLE = _S("X!demo - xeres engine test -");

#include "lib/system/system_console.h"

int APIENTRY WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , char * lpCmdLine , int nCmdShow )
{

	// Initialize GDI+.
	GdiplusStartupInput gdiplusStartupInput;
	memset( &gdiplusStartupInput , 0 , sizeof(GdiplusStartupInput) );

	gdiplusStartupInput.GdiplusVersion = 1;
	gdiplusStartupInput.DebugEventCallback = NULL;
	gdiplusStartupInput.SuppressBackgroundThread = FALSE;
	gdiplusStartupInput.SuppressExternalCodecs = FALSE;

	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	HRESULT hRes = ::CoInitialize(NULL);
	XS_ASSERT(SUCCEEDED(hRes));

	// register window class
	WNDCLASSEX wcex = {
		sizeof(WNDCLASSEX) ,
		CS_HREDRAW | CS_VREDRAW ,
		(WNDPROC) DefWindowProc ,
		0 , 0 , hInstance ,
		::LoadIcon(NULL,IDI_APPLICATION) ,
		::LoadCursor(NULL,IDC_ARROW) ,
		(HBRUSH)(COLOR_WINDOW+1) ,
		NULL , APP_NAME.c_str() ,
		::LoadIcon(NULL,IDI_APPLICATION)
	};

	if( !::RegisterClassEx(&wcex) )
	{
		__win32_error( _S("Failed to register window class : %s.") );
		return false;
	}

	// create game window
	HWND hWnd = ::CreateWindow(
		APP_NAME.c_str() ,
		APP_TITLE.c_str() ,
		WS_POPUP | WS_BORDER ,
		0 , 0 , 800 , 600 ,
		NULL ,
		NULL ,
		hInstance ,
		0 );

	if( hWnd == NULL )
	{
		__win32_error( _S("Failed to create window : %s.") );
		return false;
	}

	// give engine to control
	XDemoApp gameApp( hWnd );
	gameApp.Run( hWnd );

	// game end
	::DestroyWindow( hWnd );

	// un register window class
	::UnregisterClass( APP_NAME.c_str() , hInstance );

	// uninitialize com.
	::CoUninitialize();

	// shut down GDI+
	GdiplusShutdown(gdiplusToken);

	// wait for all resource released
	for( int i = 0 ; i < 100 ; ++i )
		::Sleep(0);

	return 0;
}
